package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.view.WheelView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyBmiBean;
import flc.ast.databinding.ActivityBmiCalculateBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.view.StkTextView;

public class BmiCalculateActivity extends BaseAc<ActivityBmiCalculateBinding> {

    private Dialog mySelHeightDialog;
    private WheelView wheelView1, wheelView2;
    private int height = 175, weight = 70;

    @Override
    protected int onCreate() {
        return R.layout.activity_bmi_calculate;
    }

    @Override
    protected void initView() {
        mDataBinding.ivBmiCalculateBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivList.setOnClickListener(this);
        mDataBinding.llHeight.setOnClickListener(this);
        mDataBinding.llWeight.setOnClickListener(this);
        mDataBinding.tvStartCal.setOnClickListener(this);

        //选择身高体重弹框
        selHeightDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivList://计算列表
                startActivity(BmiListActivity.class);
                break;
            case R.id.llHeight://身高
            case R.id.llWeight://体重
                mySelHeightDialog.show();
                break;
            //选择身高体重弹框
            case R.id.tvDialogSelHeightRight://确定
                mySelHeightDialog.dismiss();
                height = wheelView1.getCurrentItem() + 50;
                weight = wheelView2.getCurrentItem() + 10;
                mDataBinding.tvHeight.setText(height + "");
                mDataBinding.tvWeight.setText(weight + "");
                break;
            case R.id.tvStartCal://开始计算
                calculate();
                break;
        }
    }

    @Override
    protected void initData() {

    }


    //计算
    private void calculate() {
        float heightM = height / 100.0f;  // 厘米转米
        float bmi = weight / (heightM * heightM);
        String formatted = String.format("%.1f", bmi);
        mDataBinding.tvBmi.setText(formatted);
        if (bmi < 18.5) {
            mDataBinding.tvWeightType.setText(getString(R.string.weight_type1));
        } else if (bmi < 24) {
            mDataBinding.tvWeightType.setText(getString(R.string.weight_type2));
        } else if (bmi < 28) {
            mDataBinding.tvWeightType.setText(getString(R.string.weight_type3));
        } else {
            mDataBinding.tvWeightType.setText(getString(R.string.weight_type4));
        }
        saveRecord(bmi);
    }

    //保存记录
    private void saveRecord(float bmi) {
        //选择的时间
        //准备数据
        List<MyBmiBean> listSave = new ArrayList<>();
        listSave.add(new MyBmiBean(TimeUtils.getNowMills(), height, weight, bmi));
        List<MyBmiBean> listSp = AppSpUtils.getBmiList();
        if (listSp != null && listSp.size() != 0) {
            listSp.addAll(listSave);
            AppSpUtils.savBmiList(listSp);
        } else {
            AppSpUtils.savBmiList(listSave);
        }
    }


    //选择身高体重弹框
    private void selHeightDialog() {
        mySelHeightDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_sel_height, null);
        mySelHeightDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = mySelHeightDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.CENTER);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        lp.width = (int) (dm.widthPixels * 1);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        StkTextView dialogRight = inflate.findViewById(R.id.tvDialogSelHeightRight);
        wheelView1 = inflate.findViewById(R.id.wheel_view1);
        wheelView2 = inflate.findViewById(R.id.wheel_view2);
        dialogRight.setOnClickListener(this);
        wheelView1.setDividerColor(Color.parseColor("#FFFFFF"));
        wheelView1.setTextColorCenter(Color.parseColor("#000000"));
        wheelView1.setLineSpacingMultiplier(2.5f);
        wheelView1.setDividerType(WheelView.DividerType.WRAP);
        wheelView1.setCurrentItem(125);
        wheelView1.setItemsVisibleCount(3);
        wheelView1.setCyclic(true);
        wheelView1.setTextSize(32);
        wheelView2.setDividerColor(Color.parseColor("#FFFFFF"));
        wheelView2.setTextColorCenter(Color.parseColor("#000000"));
        wheelView2.setLineSpacingMultiplier(2.5f);
        wheelView2.setDividerType(WheelView.DividerType.WRAP);
        wheelView2.setCurrentItem(60);
        wheelView2.setItemsVisibleCount(3);
        wheelView2.setCyclic(true);
        wheelView2.setTextSize(32);
        //设置滚轮数据
        List<String> listHeight = new ArrayList<>();//身高
        for (int i = 50; i <= 300; i++) {
            listHeight.add(String.valueOf(i));
        }
        List<String> listWeight = new ArrayList<>();//体重
        for (int i = 10; i < 300; i++) {
            listWeight.add(String.valueOf(i));
        }
        WheelAdapter adapter1 = new ArrayWheelAdapter(listHeight);
        wheelView1.setAdapter(adapter1);
        WheelAdapter adapter2 = new ArrayWheelAdapter(listWeight);
        wheelView2.setAdapter(adapter2);
    }

}